{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Default Values - Beware!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-08-22 04:04:17.700303\n"
     ]
    }
   ],
   "source": [
    "print(datetime.utcnow())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def log(msg, *, dt=datetime.utcnow()):\n",
    "    print('{0}: {1}'.format(dt, msg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-08-22 04:04:18.406943: message 1\n"
     ]
    }
   ],
   "source": [
    "log('message 1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001-01-01 00:00:00: message 2\n"
     ]
    }
   ],
   "source": [
    "log('message 2', dt='2001-01-01 00:00:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-08-22 04:04:18.406943: message 3\n"
     ]
    }
   ],
   "source": [
    "log('message 3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-08-22 04:04:18.406943: message 4\n"
     ]
    }
   ],
   "source": [
    "log('message 4')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you can see, the default for **dt** is calculated when the function is **defined** and is **NOT** re-evaluated when the function is called."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Solution Pattern"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here is one pattern we can use to achieve the desired result:\n",
    "\n",
    "We actually set the default to None - this makes the argument optional, and we can then test for None **inside** the function and default to the current time if it is None."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "def log(msg, *, dt=None):\n",
    "    dt = dt or datetime.utcnow()\n",
    "    # above is equivalent to:\n",
    "    #if not dt:\n",
    "    #    dt = datetime.utcnow()\n",
    "    print('{0}: {1}'.format(dt, msg))    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-08-22 04:15:11.797640: message 1\n"
     ]
    }
   ],
   "source": [
    "log('message 1')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-08-22 04:15:14.529496: message 2\n"
     ]
    }
   ],
   "source": [
    "log('message 2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2001-01-01 00:00:00: message 3\n"
     ]
    }
   ],
   "source": [
    "log('message 3', dt='2001-01-01 00:00:00')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2017-08-22 04:15:18.045607: message 4\n"
     ]
    }
   ],
   "source": [
    "log('message 4')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
